package com.hyg.common.utils.glide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.hyg.common.R;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Author: huangyuguang
 * Date: 2022/5/5
 */
public class GlideUtils {

    public static void loadImage(Context context, String url, ImageView imageView) {
        loadImage(context,url,0,imageView);
//        getLoader(context, url)
//                .apply(new RequestOptions().placeholder(R.drawable.message_image_bg).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
//                        .error(R.drawable.message_image_bg).centerCrop().dontAnimate()).into(imageView);
    }

    public static void loadImage(Context context, String url, int imgResId,ImageView imageView) {
        getLoader(context, url)
                .apply(new RequestOptions().placeholder(imgResId).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .error(imgResId).centerCrop().dontAnimate()).into(imageView);
    }

    public static void loadGifImage(Context context, String url,ImageView imageView) {
        Glide.with(context).asGif().load(getFixUrl(url))
                .apply(new RequestOptions().placeholder(0).error(0).diskCacheStrategy(DiskCacheStrategy.NONE))
                .into(imageView);
    }

    /**
     * 加载普通图片，不使用加载圈
     */
    public static void loadImageHasHolder(Context context, String url, ImageView imageView) {
        getLoader(context, url)
                .apply(new RequestOptions().placeholder(R.color.light_gray_7).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .error(R.color.light_gray_7).centerCrop().dontAnimate()).into(imageView);
    }

    public static void loadCircleImage(Context context, String url, ImageView imageView) {
        loadCircleImage(context,44,url,imageView);
    }

    /**
     * 加载为圆形图片（一般为头像加载）
     */
    public static void loadCircleImage(Context context,int dpWidth,  String url, ImageView imageView) {
        loadCircleImage(context,dpWidth, R.drawable.lib_head_user_icon,url,imageView);
    }

    /**
     * 加载为圆形图片（一般为头像加载）
     */
    public static void loadCircleImage(Context context,int dpWidth, int resId, String url, ImageView imageView) {
        int headImageSize = (int) (dpWidth * context.getResources().getDisplayMetrics().density);//头像大小
        getLoader(context, url)
                .apply(new RequestOptions()
                        .placeholder(R.drawable.lib_head_user_icon)
                        .override(headImageSize, headImageSize)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .error(R.drawable.lib_head_user_icon)
                        .dontAnimate().
                                transform(new CropCircleTransformation())).into(imageView);
    }

    public static void loadCircleImageDefaultWhite(Context context, String url, ImageView imageView) {
        loadCircleImageDefaultWhite(context,44,url,imageView);
    }

    /**
     * 加载为圆形图片（一般为头像加载）
     */
    public static void loadCircleImageDefaultWhite(Context context, int dpWidth,String url, ImageView imageView) {
        int headImageSize = (int) (dpWidth * context.getResources().getDisplayMetrics().density);//头像大小
        getLoader(context, url)
                .apply(new RequestOptions()
                        .placeholder(R.color.bg_grey_f5f5f5)
                        .override(headImageSize, headImageSize)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .error(R.color.bg_grey_f5f5f5)
                        .dontAnimate().
                                transform(new CropCircleTransformation())).into(imageView);
    }

    /**
     * 加载为圆形图片,自定义placeholder和error
     */
    public static void loadCircleImageWithHolder(Context context, String url, ImageView imageView, int placeholder, int error) {
        int headImageSize = (int) (44 * context.getResources().getDisplayMetrics().density);//头像大小
        getLoader(context, url).apply(new RequestOptions()
                .placeholder(placeholder)
                .override(headImageSize, headImageSize)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(error)
                .dontAnimate()
                .transform(new CropCircleTransformation())).into(imageView);
    }

    public static void loadImageHolderColor(Context context, String url, ImageView imageView) {
        getLoader(context, url)
                .apply(new RequestOptions().placeholder(R.color.light_gray_7).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .error(R.color.light_gray_7).centerCrop().dontAnimate()).into(imageView);
    }

    /**
     * 加载Gif，不使用加载圈
     */
    public static void loadImgOrGifHolderColor(Context context, String url, ImageView imageView) {
        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        loadImgOrGifHolderColor(context,url,imageView,layoutParams.width,layoutParams.height);
    }

    /**
     * 加载Gif，不使用加载圈
     */
    public static void loadGifHolderColor(Context context, String url, ImageView imageView,int width,int height) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.color.light_gray_7)
                .error(R.color.light_gray_7)
                .override(width,height);

        Glide.with(context).asGif().load(getGlideMode(url))
                .apply(options).into(imageView);

    }

    /**
     * 加载Gif，不使用加载圈
     */
    public static void loadImgOrGifHolderColor(Context context, String url, ImageView imageView,int width,int height) {
        if (!url.endsWith(".gif")) {
            loadImageHolderColor(context, url, imageView);
        } else {
            loadGifHolderColor(context, url, imageView,width,height);
        }
    }

    /**
     * 加载Gif，不使用加载圈
     */
    public static void loadImgOrGifAdapt(Context context, String url, ImageView imageView, @DrawableRes int colors) {
        if (!url.endsWith(".gif")) {
            getLoader(context, url)
                    .apply(new RequestOptions().placeholder(colors)
                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                            .error(colors).dontAnimate()).into(imageView);
        } else {
            Glide.with(context).asGif().load(getGlideMode(url))
                    .apply(new RequestOptions().placeholder(colors)
                            .error(colors)).into(imageView);
        }
    }

    /**
     * 加载Gif，不使用加载圈
     */
    public static void loadImgOrGifCrop(Context context, String url, ImageView imageView) {
        if (!TextUtils.isEmpty(url) && !url.endsWith(".gif")) {
            getLoader(context, url)
                    .apply(new RequestOptions().placeholder(R.color.light_gray_7)
                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                            .error(R.color.light_gray_7).centerCrop().dontAnimate()).into(imageView);
        } else {
            Glide.with(context).asGif().load(getGlideMode(url))
                    .apply(new RequestOptions().placeholder(R.color.light_gray_7)
                            .error(R.color.light_gray_7).centerCrop()).into(imageView);
        }
    }

    /**
     * 加载Gif，不使用加载圈
     */
    public static void loadImgOrGifCrop(Context context, String url, ImageView imageView, @DrawableRes int colors) {
        if (!url.endsWith(".gif")) {
            getLoader(context, url)
                    .apply(new RequestOptions().placeholder(colors)
                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                            .error(colors).centerCrop().dontAnimate()).into(imageView);
        } else {
            Glide.with(context).asGif().load(getGlideMode(url))
                    .apply(new RequestOptions().placeholder(colors)
                            .error(colors).centerCrop()).into(imageView);
        }
    }

    public static void loadGifImage(Context context, int resId, ImageView imageView) {
        Glide.with(context).asGif().load(resId)
                .apply(new RequestOptions().placeholder(0).error(0).diskCacheStrategy(DiskCacheStrategy.NONE))
                .into(imageView);
    }

    public static RequestBuilder<Drawable> getLoader(Context context, String url) {
        String realUrl = getFixUrl(url);
        if(realUrl == null){
            return Glide.with(context).load("");
        }else if (isHttpUrl(realUrl)) {
            return Glide.with(context).load(new ImageGlideUrl(realUrl));
//            return Glide.with(context).load(realUrl);
        } else {
            return Glide.with(context).load(realUrl);
        }
    }

    public static Object getGlideMode(String url) {
        String realUrl = getFixUrl(url);
        if(realUrl == null){
            return "";
        }else if (isHttpUrl(url)) {
            return new ImageGlideUrl(realUrl);
//            return realUrl;
        } else {
            return realUrl;
        }
    }

    public static boolean isHttpUrl(String url){
        return !TextUtils.isEmpty(url) && (url.startsWith("https://") || url.startsWith("http://"));
    }

    public static String getFixUrl(String url) {
        if (url == null) return "";
        if (url.matches("[a-zA-z]+://[^\\s]*")) {
            return url;
        }else {
            return "http://" + url;
        }
    }

}
